package com.zlx.sharelive.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.LoginCodeBean;
import com.zlx.module_base.base_api.res_data.QueryByBean;
import com.zlx.module_base.base_api.res_data.UpdatePhoneBody;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.MyApp;
import com.zlx.sharelive.R;
import com.zlx.sharelive.utils.NullTokenUtils;
import com.zlx.sharelive.utils.StringUt;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 修改手机号
 */
public class ModifyPhoneAc extends BaseAc {

    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.tv_code)
    TextView tv_code;
    @BindView(R.id.tv_area_code)
    TextView tv_area_code;

    private TimeCount time;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_modify_phone;
    }

    @Override
    public void initViews() {
        super.initViews();

        time = new TimeCount(60000, 1000);
    }

    @OnClick({R.id.iv_back, R.id.bt_comm, R.id.tv_code, R.id.ll_area_code})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_area_code:
                //区号
                startActivityForResult(new Intent(this, AreaCodeListAc.class), 20);
                break;
            case R.id.tv_code:
                if (StringUt.isEmpty(et_phone.getText().toString().trim())) {
                    Toast.makeText(this, "手机号不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    getCode();
                }
                break;
            case R.id.bt_comm:
                if (StringUt.isEmpty(et_phone.getText().toString().trim())) {
                    Toast.makeText(this, "手机号不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (StringUt.isEmpty(et_code.getText().toString().trim())) {
                    Toast.makeText(this, "验证码不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    updatePhone();
                }
                break;
        }
    }

    private void updatePhone() {
        UpdatePhoneBody updatePhoneBody = new UpdatePhoneBody();
        updatePhoneBody.setMobile((tv_area_code.getText().toString().trim() + et_phone.getText().toString().trim()).replace("+", ""));
        updatePhoneBody.setCode(et_code.getText().toString().trim());
        ApiUtil.getLoginApi().updatePhone(SPUtil.getToken(this),
                updatePhoneBody).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<QueryByBean>>() {
            @Override
            public void onSuccess(ApiResponse<QueryByBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            Toast.makeText(ModifyPhoneAc.this, "修改成功", Toast.LENGTH_SHORT).show();
                            MyApp.getInstance().backToLogin();
                        } else {
                            NullTokenUtils.isNullToken(data.getData().getCode(), data.getData().getMsg());
                        }
                    }
                }
            }
        }));
    }

    private void getCode() {
        ApiUtil.getLoginApi().login_code((tv_area_code.getText().toString().trim() + et_phone.getText().toString().trim()).replace("+", ""), "2").observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<LoginCodeBean>>() {
            @Override
            public void onSuccess(ApiResponse<LoginCodeBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            time.start();
                        } else {
                            NullTokenUtils.isNullToken(data.getData().getCode(), data.getData().getMsg());
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null == data) {
            return;
        }
        if (resultCode == 30) {
            tv_area_code.setText(data.getStringExtra("code"));
        }
    }

    public class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv_code.setTextColor(Color.parseColor("#666666"));
            tv_code.setClickable(false);
            tv_code.setText("(" + millisUntilFinished / 1000 + ") s");
        }

        @Override
        public void onFinish() {
            tv_code.setText(getResources().getText(R.string.resend_code));
            tv_code.setClickable(true);
            tv_code.setTextColor(Color.parseColor("#FF64B4"));
        }
    }
}
